package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuanetPermission;
import cn.ronghuanet.auth.domain.Role;
import cn.ronghuanet.auth.dto.RoleDTO;
import cn.ronghuanet.auth.query.RoleQuery;
import cn.ronghuanet.auth.service.IRoleService;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.apache.coyote.ajp.AjpAprProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/role")
@RonghuanetPermission(name="角色管理",desc = "管理角色增删改查")
public class RoleController {
    @Autowired
    private IRoleService service;
    // 新增角色/修改
    @PostMapping
    @RonghuanetPermission(name = "角色保存",desc = "角色新增或修改")
    public AjaxResult save(@RequestBody Role role){
        try{
            if(Objects.isNull(role.getId())){
                service.insert(role);
            }else {
                service.update(role);
            }
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }

    }
    // 删除指定角色
    @DeleteMapping("/{id}")
    @RonghuanetPermission(name = "角色删除",desc = "删除指定角色")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try{
            service.delete(id);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }

    }
    // 批量删除
    @PatchMapping
    @RonghuanetPermission(name = "角色批量删除",desc = "批量角色")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try{
            service.batchDelete(ids);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }

    }
    // 分页查询
    @PutMapping
    @RonghuanetPermission(name = "分页查询",desc = "角色分页查询")
    public AjaxResult queryPageList(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> page = service.pageList(roleQuery);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    // 分页查询
    @PostMapping("/permission")
    @RonghuanetPermission(name = "授权",desc = "给用户授权")
    public AjaxResult setRolePermission(@RequestBody RoleDTO roleDTO){
        try {
            service.setRolePermission(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 根据角色id 查询对应的权限
     * @param
     * @return
     */
    @PostMapping("/{id}")
    @RonghuanetPermission(name = "根据角色id 查询对应的权限",desc = "根据角色id 查询对应的权限")
    public AjaxResult getRolePermission(@PathVariable("id")  Long id){
        try {
            List<String> list = service.getRolePermission(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
